<template>
  <div class="app-container">
    <vue-html2pdf
      ref="html2Pdf"
      :enable-download="true"
      filename="myPdf"
      :float-layout="true"
      :manual-pagination="false"
      :paginate-elements-by-height="1400"
      pdf-content-width="100%"
      pdf-format="a4"
      pdf-orientation="portrait"
      :pdf-quality="2"
      :preview-modal="false"
      :show-layout="false"
      @hasDownloaded="hasDownloaded($event)"
      @progress="onProgress($event)"
    >
      <section slot="pdf-content">
        <div style="padding: 16px">
          <blood v-if="name === 'blood'" ref="ele" />
          <match v-if="name === 'match'" ref="ele" />
        </div>
      </section>
    </vue-html2pdf>
  </div>
</template>

<script>
  import VueHtml2pdf from 'vue-html2pdf'
  import { printData, delData } from '@/utils/constant'
  // eslint-disable-next-line no-unused-vars
  import { doPrint, doDel } from '@/api/gfPrint'
  import { doPrints } from '@/api/blood'
  // const { serverURL } = require('@/config')
  import Blood from './blood'
  import Match from './match'
  export default {
    components: {
      VueHtml2pdf,
      Blood,
      Match,
    },
    props: {
      name: {
        type: String,
        default: '',
      },
    },
    data() {
      return {
        bloods: [],
        printData,
        delData,
      }
    },
    methods: {
      async doPrints() {
        const { msg } = await doPrints({
          ids: this.bloods.map((val) => val.id),
        })
        this.$emit('close')
        this.$baseMessage(msg, 'success', 'vab-hey-message-success')
      },
      show(row) {
        if (this.name === 'blood') {
          this.bloods = row
        }
        this.$refs.ele.show(row)
        setTimeout(() => {
          this.generatePdf()
        }, 500)
      },
      async doPrint() {
        // this.saveData.pdfFilePath = serverURL + '/storage/' + saveName
        // const saveRes = await doSave(this.saveData)
        try {
          await doPrint(this.printData)
          await doDel(this.delData)
          if (this.name === 'blood') {
            this.doPrints()
          }
        } catch (e) {
          console.log(e)
          this.$baseMessage('打印服务未启动', 'error', 'vab-hey-message-error')
        }
      },
      onProgress(e) {
        console.log(e)
      },
      hasDownloaded(e) {
        const blob = new Blob([e], { type: 'text/plain;charset=utf-8' })
        const formData = new FormData()
        formData.append('pdf', blob, 'pdf.pdf')
        setTimeout(() => {
          this.doPrint()
        }, 50)
      },
      generatePdf() {
        this.$refs.html2Pdf.generatePdf()
      },
    },
  }
</script>

<style lang="scss" scoped></style>
